package com.example.qt.ptplatform.controller;

import com.example.qt.ptplatform.entity.Comments;
import com.example.qt.ptplatform.service.CommentService;
import com.example.qt.ptplatform.entity.CommentRequest;  // 导入 CommentRequest 类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/comments")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @PostMapping("/submit")
    public String submitComment(@RequestBody CommentRequest commentRequest) {

        // 打印接收到的参数，用于调试
        System.out.println("Received Parameters:");
        System.out.println("torrentId: " + commentRequest.getTorrentId());
        System.out.println("userId: " + commentRequest.getUserId());
        System.out.println("content: " + commentRequest.getContent());
        System.out.println("isAnonymous: " + commentRequest.getIsAnonymous());

        // 检查是否有必填字段为空
        if (commentRequest.getTorrentId() == null || commentRequest.getUserId() == null || commentRequest.getContent() == null || commentRequest.getContent().isEmpty()) {
            return "错误：缺少必要的字段！";
        }

        // 将 isAnonymous 转换为 Boolean 类型
        Boolean isAnonymousBoolean = "1".equals(commentRequest.getIsAnonymous());  // "1" -> true, "0" -> false

        try {
            // 调用 service 层保存评论
            commentService.saveComment(commentRequest.getTorrentId(), commentRequest.getUserId(), commentRequest.getContent(), isAnonymousBoolean);
            return "评论提交成功!";
        } catch (Exception e) {
            return "错误：" + e.getMessage();
        }
    }

    @PostMapping("/reply")
    public String submitReply(@RequestBody CommentRequest commentRequest) {

        // 打印接收到的参数，用于调试
        System.out.println("Received Parameters:");
        System.out.println("torrentId: " + commentRequest.getTorrentId());
        System.out.println("userId: " + commentRequest.getUserId());
        System.out.println("content: " + commentRequest.getContent());
        System.out.println("isAnonymous: " + commentRequest.getIsAnonymous());
        System.out.println("comCommentId: " + commentRequest.getComCommentId());  // 回复的评论 ID

        // 检查是否有必填字段为空
        if (commentRequest.getTorrentId() == null || commentRequest.getUserId() == null || commentRequest.getContent() == null || commentRequest.getContent().isEmpty() || commentRequest.getComCommentId() == null) {
            return "错误：缺少必要的字段！";
        }

        // 将 isAnonymous 转换为 Boolean 类型
        Boolean isAnonymousBoolean = "1".equals(commentRequest.getIsAnonymous());  // "1" -> true, "0" -> false

        try {
            // 调用 service 层保存回复
            commentService.saveReply(commentRequest.getTorrentId(), commentRequest.getUserId(), commentRequest.getContent(), isAnonymousBoolean, commentRequest.getComCommentId());
            return "回复提交成功!";
        } catch (Exception e) {
            return "错误：" + e.getMessage();
        }
    }

    // 使用 @RequestParam 获取 URL 参数
    @PostMapping("/like")
    public String likeComment(@RequestParam Integer commentId) {
        try {
            // 调用 service 层点赞
            commentService.likeComment(commentId);
            return "点赞成功!";
        } catch (Exception e) {
            return "错误：" + e.getMessage();
        }
    }

    // 使用 @RequestParam 获取 URL 参数
    @PostMapping("/unlike")
    public String unlikeComment(@RequestParam Integer commentId) {
        try {
            // 调用 service 层取消点赞
            commentService.unlikeComment(commentId);
            return "取消点赞成功!";
        } catch (Exception e) {
            return "错误：" + e.getMessage();
        }
    }

    @DeleteMapping("/delete")
    public String deleteComment(@RequestParam Integer commentId) {
        try {
            // 调用 service 层删除评论
            commentService.deleteComment(commentId);
            return "评论删除成功!";
        } catch (Exception e) {
            return "错误：" + e.getMessage();
        }
    }

    // 新增获取所有评论及其回复的接口
    @GetMapping("/list")
    public List<Comments> getAllComments() {
        try {
            // 获取所有评论及其回复
            return commentService.getAllCommentsWithReplies();
        } catch (Exception e) {
            throw new RuntimeException("获取评论列表失败: " + e.getMessage());
        }
    }
}
